package com.promis.widget.imgzoom;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.promis.util.ScreenUtils;
import com.promis.widget.photoview.OnViewTapListener;
import com.promis.widget.photoview.PhotoView;

import java.util.List;

/**
 * Created by public1 on 2017/5/23.
 */

public class PageImageAdapter extends BaseAdapter {

    private Context context;
    private List<Drawable> resources;

    public PageImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (this.resources != null) return resources.size();
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = new PhotoView(context);
            PhotoView imageView = (PhotoView) view;
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//            imageView.setMinimumScale(0.5f);
            imageView.setOnViewTapListener(new OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(i,view);
                    }
                }
            });
            Rect rect = ScreenUtils.getScreenRect(context);
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(rect.width(), rect.height());
            view.setLayoutParams(param);
        }

        PhotoView imageView = (PhotoView) view;
        imageView.setImageDrawable(resources.get(i));
//        Glide.with(context).load(imageUrls.get(i)).into(new ImageViewTarget(imageView));

        return view;
    }


    private OnItemClickListener onItemClickListener;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int i, View view);
    }

    public void setResources(List<Drawable> resources) {
        this.resources = resources;
    }
}
